var express = require('express')
var AdminRole = require('../db/models/AdminRole')
var DbOpt = require('../db/DbOpt')
var ResUtil = require('../db/ResUtil')
var router = express.Router()

/**
 * 添加系统角色
 */
router.post('/insertRole', function (req, res, next) {
    var json = req.body
    console.log(json)
    if (json.name && json.comments) {
        DbOpt.addOne(AdminRole, req, res, function (status) {
            res.json(ResUtil.resData(status, ''))
        }, '添加一个系统角色')
    } else {
        res.json(ResUtil.resData('fail', '', ResUtil.ERROR_PARAM))
    }
})

/**
 * 获取role列表
 */
router.get('/queryRole', function (req, res, next) {
    DbOpt.findAll(AdminRole, req, res, function (status, data) {
        res.json(ResUtil.resData(status, data))
    }, '获取role列表')
})

/**
 * 删除角色
 */
router.post('/deleteRole', function (req, res, next) {
    if (req.body._id) {
        DbOpt.del(AdminRole, req, res, function (status) {
            res.json(ResUtil.resData(status, ''))
        }, '删除角色')
    } else {
        res.json(ResUtil.resData('fail', ResUtil.ERROR_PARAM))
    }
})

/**
 * 更新角色
 */
router.post('/updateRole', function (req, res, next) {
    var json = req.body
    if (json._id && json.dto) {
        DbOpt.updateOneByID(AdminRole, req, res, function (status) {
            res.json(ResUtil.resData(status, ''))
        }, '修改一个系统角色')
    } else {
        res.json(ResUtil.resData('fail', ResUtil.ERROR_PARAM))
    }
})

module.exports = router